using System;
using System.Threading;
using System.Threading.Tasks;
using Avalonia;
using Avalonia.Controls;
using Avalonia.Interactivity;
using Avalonia.Markup.Xaml;
using RosSharp;

namespace AvaloniaSim.Views
{
    public partial class RosPublishWindow : Window
    {
        

        private bool m_HaveInitRos = false;

        private Publisher<Messages.std_msgs.String> pub;

        public RosPublishWindow()
        {
            InitializeComponent();
#if DEBUG
            this.AttachDevTools();
#endif
        }

        private void InitRos()
        {
            ROS.ROS_MASTER_URI = $"http://{txtMasterIP.Text}:{txtMasterPort.Text}";

            ROS.ROS_HOSTNAME = txtHostName.Text;
            if (!string.IsNullOrEmpty(txtRosIP.Text))
            {
                ROS.ROS_IP = txtRosIP.Text;
            }
           
          

            string nodeName = txtNodeName.Text;
            ROS.Init(nodeName);
            NodeHandle nh = new NodeHandle();
            pub = nh.Advertise<Messages.std_msgs.String>(txtTopic.Text, 1, false);
            m_HaveInitRos = true;
        }

        

        private void BtnRosSimPub_OnClick(object sender, RoutedEventArgs e)
        {
            if (m_HaveInitRos==false)
            {
                InitRos();
            }
            
            pub.Publish(new Messages.std_msgs.String { data = txtPub.Text });
            

        }

        private void BtnRosPub_OnClick(object sender, RoutedEventArgs e)
        {

            if (m_HaveInitRos == false)
            {
                InitRos();
            }

            Task thread = new Task(() =>
            {
                int i = 0;
                while (ROS.ok)
                {
                    i++;
                    pub.Publish(new Messages.std_msgs.String { data = "Message" + i });
                    if (i == 65536)
                        i = 0;
                    Thread.Sleep(1000);
                }
            });
            thread.Start();

        }

        private void BtnRosSub_OnClick(object sender, RoutedEventArgs e)
        {
            //this.txtSub.NewLine = Guid.NewGuid().ToString();
            //return;
            ROS.ROS_MASTER_URI = $"http://{txtMasterIP.Text}:{txtMasterPort.Text}";
            ROS.ROS_HOSTNAME = txtHostName.Text;
            if (!string.IsNullOrEmpty(txtRosIP.Text))
            {
                ROS.ROS_IP = txtRosIP.Text;
            }
            else
            {
                
            }
           

            string nodeName = txtNodeName.Text;
            ROS.Init(nodeName);
            NodeHandle nh = new NodeHandle();
            Subscriber<Messages.std_msgs.String> sub = nh.Subscribe<Messages.std_msgs.String>(txtTopic.Text,10, subCallback);
        }

        private   void subCallback(Messages.std_msgs.String argument)
        {
            //TextBox text = new TextBox();
            //text.NewLine = "";
            Avalonia.Threading.Dispatcher.UIThread.InvokeAsync(() =>
                {
                    //this.txtSub.NewLine= argument.data;
                    this.txtSub.Text += argument.data+"\r\n";
                }
                );

        }
    }
}
